package com.zzyl.controller;

import cn.hutool.json.JSONUtil;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.UserDto;
import com.zzyl.service.UserService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @FileName UserController
 * @Description
 * @Author Sword
 * @date 2025-10-08
 **/

@Slf4j
@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户分页
     *
     * @param userDto
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping("/page/{pageNum}/{pageSize}")
    @ApiOperation("用户分页")
    public ResponseResult<PageResponse<UserVo>> page(@RequestBody UserDto userDto, @PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        log.info("用户分页：{},{},{}", userDto, pageNum, pageSize);
        PageResponse<UserVo> pageResponse = userService.page(userDto, pageNum, pageSize);
        return ResponseResult.success(pageResponse);
    }

    /**
     * 用户列表
     *
     * @param userDto
     * @return
     */
    @PostMapping("/list")
    @ApiOperation("用户列表")
    public ResponseResult<List<UserVo>> list(@RequestBody UserDto userDto) {
        log.info("用户列表：{}", userDto);
        List<UserVo> userVoList = userService.list(userDto);
        return ResponseResult.success(userVoList);
    }

    /**
     * 用户添加
     *
     * @param userDto
     * @return
     */
    @PutMapping
    @ApiOperation("用户添加")
    public ResponseResult add(@RequestBody UserDto userDto) {
        log.info("用户添加：{}", userDto);
        userService.add(userDto);
        return ResponseResult.success();
    }

    /**
     * 用户修改
     *
     * @param userDto
     * @return
     */
    @PatchMapping
    @ApiOperation("用户修改")
    public ResponseResult updateUser(@RequestBody UserDto userDto) {
        log.info("用户修改:{}", userDto);
        userService.updateUser(userDto);
        return ResponseResult.success();
    }

    /**
     * 删除用户
     *
     * @param userIds
     * @return
     */
    @DeleteMapping("/remove/{userIds}")
    @ApiOperation("删除用户")
    public ResponseResult remove(@PathVariable String userIds) {
        log.info("删除用户:{}", userIds);
        userService.remove(userIds);
        return ResponseResult.success();
    }

    /**
     * 启用或禁用用户
     *
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/is-enable/{id}/{status}")
    @ApiOperation("启用或禁用用户")
    public ResponseResult isEnable(@PathVariable Long id, @PathVariable String status) {
        log.info("启用或禁用用户:{},{}", id, status);
        UserDto userDto = new UserDto();
        userDto.setId(id);
        userDto.setDataState(status);
        userService.isEnable(userDto);
        return ResponseResult.success();
    }

    /**
     * 当前用户
     *
     * @return
     */
    @GetMapping("/current-user")
    @ApiOperation("当前用户")
    public ResponseResult<UserVo> currentUser() {
        Long userId = UserThreadLocal.getMgtUserId();
        log.info("当前用户:{}", userId);
        UserVo userVo = userService.currentUser(userId);
        return ResponseResult.success(userVo);
    }

    /**
     * 密码重置
     *
     * @param userId
     * @return
     */
    @PostMapping("/reset-passwords/{userId}")
    @ApiOperation("密码重置")
    public ResponseResult resetPasswords(@PathVariable Long userId) {
        log.info("密码重置:{}", userId);
        userService.resetPasswords(userId);
        return ResponseResult.success();
    }

}
